package cn.dly.login.controller;





import cn.dly.login.entity.PointsDetails;
import cn.dly.login.exception.LRException;
import cn.dly.login.utils.JwtUtils;
import cn.dly.login.utils.R;
import cn.dly.login.vo.InfoVo;
import cn.dly.login.vo.LoginVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

@CrossOrigin
@RequestMapping("/sys/user/lr")
@RestController
@Api(tags = "个人信息")
public class PerInfoController {

    @Autowired
    private cn.dly.login.service.UserService userService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Resource
    private RestTemplate restTemplate;

    @ApiOperation("查询用户信息")
    @GetMapping("query")
    public R query(HttpServletRequest request) {
        try {
            String username = JwtUtils.getUserUNByJwtToken(request);
            InfoVo infoVo = userService.getInfoVo(username);
            return R.ok().data("infoVo", infoVo);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LRException(20001, "error");
        }
    }

    @ApiOperation("修改用户信息")
    @PostMapping("modifyInfo")
    public R modifyInfo(InfoVo infoVo,HttpServletRequest request) {
        String username = JwtUtils.getUserUNByJwtToken(request);
        int res = userService.modify(infoVo,username);
        if (res == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation("上传头像")
    @PostMapping("upload")
    public R upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
        String url = "http://10.1.188.6:8081/upload/uploadFile";
        MultiValueMap<String, Object> bodyMap = new LinkedMultiValueMap<>();
        bodyMap.add("file", new FileSystemResource(convert(file)));
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(bodyMap, headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> response = restTemplate.exchange(url,
                HttpMethod.POST, requestEntity, String.class);
        System.out.println(response.getBody());
        String back = response.getBody();
        if(back!=null){
            String username = JwtUtils.getUserUNByJwtToken(request);
            int res=userService.upload(back,username);
            System.out.println(username+"我是一个username");
            if(res==1){
                return R.ok().data("url",back);
            }
            else{
                return R.error();
            }
        }
        else{
            return R.error();
        }
    }

    public static File convert(MultipartFile file)
    {
        File convFile = new File("temp_image", file.getOriginalFilename());
        if (!convFile.getParentFile().exists()) {
            System.out.println("mkdir:" + convFile.getParentFile().mkdirs());
        }
        try {
            convFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(convFile);
            fos.write(file.getBytes());
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return convFile;
    }

    @ApiOperation("详情")
    @PostMapping("details")
    public R details(HttpServletRequest request) {
        String username = JwtUtils.getUserUNByJwtToken(request);
        List<PointsDetails> list=userService.details(username);
        if(list!=null){
            return R.ok().data("data",list);
        }
        else{
            return R.error();
        }
    }

    @ApiOperation("验证原始密码")
    @PostMapping("password")
    public R getJpassword(HttpServletRequest request, LoginVo loginVo) {
        String username = JwtUtils.getUserUNByJwtToken(request);
        boolean back=userService.yzmm(username,loginVo.getPassword());
        if(back){
            return R.ok();
        }
        else{
            return R.error();
        }
    }

    @ApiOperation("修改密码")
    @PostMapping("modpassword")
    public R modpass(HttpServletRequest request, LoginVo loginVo) {
        String username = JwtUtils.getUserUNByJwtToken(request);
        int count=userService.modp(username,loginVo.getPassword());
        if(count==1){
            return R.ok();
        }
        else{
            return R.error();
        }
    }

    @ApiOperation("获取用户头像")
    @PostMapping("geturl")
    public R geturl(LoginVo loginVo) {
        String username = loginVo.getUsername();
        String url=userService.geturl(username);
        if(url==null){
            return R.error();
        }
        else{
            return R.ok().data("url",url);
        }
    }

    @ApiOperation("获取所有用户列表")
    @GetMapping("getall")
    public R getall() {
        List<InfoVo> is=userService.getall();
        if(is==null){
            return R.error();
        }
        else{
            return R.ok().data("list",is);
        }
    }

    @ApiOperation("超级管理员")
    @PostMapping("modall")
    public R modall(InfoVo infoVo) {
        if(infoVo.getUsername()==null){
            return R.error();
        }
        int count=userService.modall(infoVo);
        if(count==1){
            return R.ok();
        }
        else{
            return R.error();
        }
    }

}
